/**
 * @author 云离
 * @date: 2018/12/28
 */
import React, { Component } from "react";
import PropTypes from "prop-types";
import md5 from "md5-node";
import { Modal, Form, Input, message } from "antd";
import axios from "../../../../apis/api";
import qs from "qs";

const formItemLayout = {
  labelCol: {
    xs: { span: 24 },
    sm: { span: 8 }
  },
  wrapperCol: {
    xs: { span: 24 },
    sm: { span: 14 }
  }
};

class AddUserForm extends Component {
  static propTypes = {
    visible: PropTypes.bool.isRequired,
    okCb: PropTypes.func.isRequired,
    cancelCb: PropTypes.func.isRequired,
    title: PropTypes.string.isRequired,
    initForm: PropTypes.object.isRequired
  };

  componentWillReceiveProps(nextProps, nextContext) {
    if (nextProps.visible && !this.props.visible) {
      this.props.form.resetFields();
    }
  }

  handleOk = () => {
    this.props.form.validateFields(async (err, values) => {
      if (!err) {
        if (this.props.cardFormType === "add") {
          try {
            const data = { ...values, deptId: 122 };
            data.password = md5(data.password);
            const result = await axios.post(
              "/registerByAdmin",
              qs.stringify(data)
            );
            if (result.code === 0) {
              message.success(result.msg);
              this.props.okCb(result);
            } else {
              message.error(result.msg);
            }
          } catch (e) {
            console.log(e);
            message.error(e.message || e.msg);
          }
        } else {
          const { initForm } = this.props;
          try {
            const data = {
              userId: initForm.userId,
              businessId: initForm.userId,
              deptId: 122,
              userName: values.userName,
              invitationCode: values.invitationCode,
              status: values.status ? 0 : 1
            };
            const result = await axios.post(
              "/channel/businessPauseOrderSetting/edit",
              qs.stringify(data)
            );
            if (result.code === 0) {
              message.success(result.msg);
              this.cancelCardForm();
              this.handlePageChange(1);
            } else {
              message.error(result.msg);
            }
          } catch (e) {
            console.log(e);
            message.error(e.message || e.msg);
          }
        }
      }
    });
  };

  render() {
    const { visible, cancelCb, title, initForm } = this.props;
    const { getFieldDecorator } = this.props.form;

    return (
      <Modal
        title={title}
        visible={visible}
        onOk={this.handleOk}
        onCancel={cancelCb}
        okText={"确认"}
        cancelText={"取消"}
      >
        <Form {...formItemLayout}>
          <Form.Item label={"用户名称"}>
            {getFieldDecorator("userName", {
              initialValue: initForm["userName"] || ""
            })(<Input />)}
          </Form.Item>
          <Form.Item label={"邀请码"}>
            {getFieldDecorator("invitationCode", {
              initialValue: initForm["invitationCode"] || ""
            })(<Input />)}
          </Form.Item>
          {/*{this.props.cardFormType !== "add" && (*/}
          {/*  <Form.Item label={"用户状态"}>*/}
          {/*    {getFieldDecorator("status", {*/}
          {/*      initialValue: initForm["status"] || true*/}
          {/*    })(<Switch checkedChildren="开" unCheckedChildren="关" />)}*/}
          {/*  </Form.Item>*/}
          {/*)}*/}
          {this.props.cardFormType === "add" && (
            <Form.Item label={"登录密码"}>
              {getFieldDecorator("password", {
                initialValue: initForm["password"] || "123456"
              })(<Input type={"password"} />)}
            </Form.Item>
          )}
        </Form>
      </Modal>
    );
  }
}

export default Form.create()(AddUserForm);
